///////////////////////////////////////////////////////////////////////////////
//Written by Liu Zhengxi 
//May 5,2003
//Compiles with Visual C++ 6.0 for Windows 98 and probably Windows 2000 
// too.
/////////////////////////////////////////////////////////////////////////////

//
// Splash.cpp : implementation file
//

#include <atlbase.h>
#include <afxwin.h>
#include <afxpriv2.h>
#include "stdafx.h"  // e. g. stdafx.h
#include "Splash.h"  // e.g. splash.h

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
//   CSplashWnd class

////////////////////////////////////////////////////////////////////////////
//constructor
//Load image from the given file
//
CSplashWnd::CSplashWnd(UINT nIDRes)
{
//	if (!m_bitmap.LoadBitmap(nIDRes))
	m_strVersion = "";
	fileIsValid = m_bitmap.LoadBitmap(nIDRes);
	if(fileIsValid)
	{
// 		CSize cz = pic.GetImageSize(NULL);
// 		width = cz.cx;
// 		height = cz.cy;
// 		BITMAP bm;
// 		m_bitmap.GetBitmap(&bm);
	}

}


////////////////////////////////////////////////////////////////////////////////
//nothing to do
//deconstructor
//
CSplashWnd::~CSplashWnd()
{
}

////////////////////////////////////////////////////////////////////////////////
//message map
//
BEGIN_MESSAGE_MAP(CSplashWnd, CWnd)
	//{{AFX_MSG_MAP(CSplashWnd)
	ON_WM_CREATE()
	ON_WM_TIMER()
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


////////////////////////////////////////////////////////////////////////////////
//ShowSplash
//to display the given image on screen
//
BOOL CSplashWnd::ShowSplash()
{
	
	if(fileIsValid)
	{
		if (!Create(AfxGetMainWnd()))
			return false;
		else
		{
			UpdateWindow();
			return true;
		}
	}
	else
	{
		return false;
	}
}


////////////////////////////////////////////////////////////////////////////////
//PreTranslateAppMessage
//
BOOL CSplashWnd::PreTranslateAppMessage(MSG* pMsg)
{
	// If we get a keyboard or mouse message, hide the splash screen.
	if (pMsg->message == WM_KEYDOWN ||
	    pMsg->message == WM_SYSKEYDOWN ||
	    pMsg->message == WM_LBUTTONDOWN ||
	    pMsg->message == WM_RBUTTONDOWN ||
	    pMsg->message == WM_MBUTTONDOWN ||
	    pMsg->message == WM_NCLBUTTONDOWN ||
	    pMsg->message == WM_NCRBUTTONDOWN ||
	    pMsg->message == WM_NCMBUTTONDOWN)
	{
		CloseSplash();
		return TRUE;	// message handled here
	}

	return FALSE;	// message not handled
}


////////////////////////////////////////////////////////////////////////////////
//Create
//make a popup splash window
//
BOOL CSplashWnd::Create(CWnd* pParentWnd /*= NULL*/)
{
	BITMAP bm;
	m_bitmap.GetBitmap(&bm);

	return CreateEx(0,
		AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
		NULL, WS_POPUP | WS_VISIBLE, 0, 0, bm.bmWidth, bm.bmHeight, pParentWnd->GetSafeHwnd(), NULL);

	
// 	return CreateEx(0,
// 		AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
// 		NULL, WS_POPUP | WS_VISIBLE, 0, 0, width, height, pParentWnd->GetSafeHwnd(), NULL);
}


////////////////////////////////////////////////////////////////////////////////
//CloseSplash
//Quit the splash window
//
void CSplashWnd::CloseSplash()
{
	// Destroy the window, and update the mainframe.
	DestroyWindow();
}


////////////////////////////////////////////////////////////////////////////////
//do nothing
//
void CSplashWnd::PostNcDestroy()
{

}

////////////////////////////////////////////////////////////////////////////////
//OnCreate
//put the splash window on center
//
int CSplashWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	// Center the window.
	CenterWindow();
	SetWindowPos(&this->wndTop,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);

	GetVersion();
	return 0;
}

////////////////////////////////////////////////////////////////////////////////
//OnPaint
//Display the given image
//
void CSplashWnd::OnPaint()
{
	CPaintDC dc(this);
	// TODO: Add your message handler code here
	if(fileIsValid)
	{

		CDC dcImage;
		if (!dcImage.CreateCompatibleDC(&dc))
			return;

		BITMAP bm;
		m_bitmap.GetBitmap(&bm);

		// Paint the image.
		CBitmap* pOldBitmap = dcImage.SelectObject(&m_bitmap);
		dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &dcImage, 0, 0, SRCCOPY);
		dcImage.SelectObject(pOldBitmap);
		DeleteDC(dcImage);
	}
	// Do not call CWnd::OnPaint() for painting messages
}


////////////////////////////////////////////////////////////////////////////////
//ShowText
//sometimes if we show what we are doing (I display the information on the center of //the picture ), the customer will be more patient
//
//
void CSplashWnd::ShowText(LPCTSTR lpStr)
{
	if(fileIsValid)
	{
		CPaintDC dc(this);
//		Sleep(2000);
		CDC dcImage;
		if (!dcImage.CreateCompatibleDC(&dc))
			return;

		BITMAP bm;
		m_bitmap.GetBitmap(&bm);

		// Paint the image.
		CBitmap* pOldBitmap = dcImage.SelectObject(&m_bitmap);
		dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &dcImage, 0, 0, SRCCOPY);
		dcImage.SelectObject(pOldBitmap);
		dcImage.DeleteDC();
 		dc.SetBkMode(TRANSPARENT);
		
		Invalidate();


//		SIZE sz;
//		sz = (SIZE)dc.GetTextExtent(lpStr,strlen(lpStr));
//		dc.TextOut(100,100,"                                                                        ");
		dc.TextOut(120,220,lpStr);
		dc.TextOut(20,350,m_strVersion);

//		dc.TextOut((width-sz.cx)/2,height/2 - 100,lpStr);
	}
}


CString CSplashWnd::GetVersion()
{
	DWORD dwVerInfoSize = 0;
	DWORD dwVerHnd;
	VS_FIXEDFILEINFO * pFileInfo;
	TCHAR szFullPath[MAX_PATH];
	
	GetModuleFileName(NULL, szFullPath, sizeof(szFullPath));
    dwVerInfoSize = GetFileVersionInfoSize(szFullPath, &dwVerHnd);
    if (dwVerInfoSize)
    {
        // If we were able to get the information, process it:
        HANDLE  hMem;
        LPVOID  lpvMem;
		unsigned int uInfoSize = 0;
		
        hMem = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
        lpvMem = GlobalLock(hMem);
        GetFileVersionInfo(szFullPath, dwVerHnd, dwVerInfoSize, lpvMem);
		
		::VerQueryValue(lpvMem, (LPTSTR)_T("\\"), (void**)&pFileInfo, &uInfoSize);
		
		WORD m_nProdVersion[4];
		
		// Product version from the FILEVERSION of the version info resource 
		m_nProdVersion[0] = HIWORD(pFileInfo->dwProductVersionMS); 
		m_nProdVersion[1] = LOWORD(pFileInfo->dwProductVersionMS);
		m_nProdVersion[2] = HIWORD(pFileInfo->dwProductVersionLS);
		m_nProdVersion[3] = LOWORD(pFileInfo->dwProductVersionLS); 

//		WORD m_nFileVersion = LOWORD(pFileInfo->dwFileVersionLS);
//		m_strVersion.Format(_T("Ver: %d.%d.%d.%d ( Build:%d )"),m_nProdVersion[0],
//			m_nProdVersion[1],m_nProdVersion[2],m_nProdVersion[3],m_nFileVersion);

		m_strVersion.Format(_T("Ver: %d.%d.%d.%d"),m_nProdVersion[0],
			m_nProdVersion[1],m_nProdVersion[2],m_nProdVersion[3]);
	
		
		GlobalUnlock(hMem);
        GlobalFree(hMem);

	}
	return m_strVersion;

}

BOOL CSplashWnd::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class

	m_bitmap.DeleteObject();
	return CWnd::DestroyWindow();
}
